﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web_App___AWS_ELB_Monitor.Models.Alarms;

namespace Web_App___AWS_ELB_Monitor.Controllers
{
    public class AlarmsController : Controller
    {
        //
        // GET: /CW/

        public ActionResult Index()
        {
            //get all alarms
            var vm = new AlarmsIndexViewModel();

            return View("Index", vm);
            //send to view
        }

        public ActionResult AlarmsData()
        {
            var alarmInfo = CloudWatchDataHelper.GetAlarms();
            return Json(alarmInfo.Select(alarm => new Alarm
            {
                Arn = alarm.AlarmArn,
                Name = alarm.AlarmName,
                Threshold = alarm.Threshold
            }),JsonRequestBehavior.AllowGet);
        }

        public ActionResult AlarmData(string alarmArn)
        {
            var alarm = CloudWatchDataHelper.GetAlarm(alarmArn);
            
            return Json(CloudWatchDataHelper.GetAlarmDataMultipleTimeSpans(alarm, DateTimeOffset.Now, DateTimeOffset.Now.AddDays(-01), DateTimeOffset.Now.AddDays(-07))
                                                      .ToDatePointGraph().Select(graph =>
                                            new
                                            {
                                                Date = graph.TimeStamp.ToString("HH:mm"),
                                                Data = graph.Value
                                            }
                                            ), JsonRequestBehavior.AllowGet);
        }
    }
}
